#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gptimer.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "TimerConfig.h"
#include "HC_SR04.h"



static const char *TAG = "Timer";
#define GPIO_BLINK_START BIT0
#define MeasureDis_START BIT1
#define MQTTIno_Upload_START  BIT2
#define OTA_Upload_START  BIT3   //该事件位是为了测试连续OTA功能是否完整

#define GPIO11_LED GPIO_NUM_11   //LED pin
#define BOOT_GPIO GPIO_NUM_9

EventGroupHandle_t timer_event_group; //定时器事件组句柄








/****************************************************
 * 定时器配置初始化
 * 功能：每10s进行一次测距  每20s进行一次数据上传
 * 
 ****************************************************/

void TimerConfigInit(void);

/*每一秒触发一次   此处设定的是每10s测一次距离，后续需要交付时需要更改*/
static bool IRAM_ATTR example_timer_on_alarm_cb_v1(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data)
{
    BaseType_t high_task_awoken = pdFALSE;
    static char button_down_3s = 0;
    // static char check_button_down_10s = 0;



    if(gpio_get_level(BOOT_GPIO) == 0){

        button_down_3s++;

        if(3 < button_down_3s && gpio_get_level(BOOT_GPIO) == 0){
        button_down_3s = 0;
        //初始化NVS
        
         ESP_ERROR_CHECK(nvs_flash_erase());
            //重新尝试初始化
         ESP_LOGI(TAG, "NVS_FLASH_ERASE_FINISH");
        
            esp_restart();
        }
        
    } else{
        button_down_3s = 0;
    }


  

    //xEventGroupSetBitsFromISR(timer_event_group, GPIO_BLINK_START, NULL);   //测试用的，设置灯闪烁
    //xEventGroupSetBitsFromISR(timer_event_group, MeasureDis_START, NULL);   //测距开始事件位置位
    static uint8_t count10s = 0, count20s = 0, count60s = 0;
    count10s++;
    count20s++;
    count60s++;
    if( 10 <= count10s){
        count10s = 0;
        // xEventGroupSetBitsFromISR(timer_event_group, MeasureDis_START, NULL);   //测距开始事件位置位  测试时屏蔽该行避免频繁测距
        
    }

    if( 20 <= count20s){
        count20s = 0;
        // xEventGroupSetBitsFromISR(timer_event_group, MQTTIno_Upload_START, NULL);   //上传mqtt事件位现在暂时不用
    }

    if( 60 <= count60s){
        count60s = 0;
        // xEventGroupSetBitsFromISR(timer_event_group, OTA_Upload_START, NULL);   //
    }

    return (high_task_awoken == pdTRUE);
}



    
void TimerConfigInit()
{
    timer_event_group = xEventGroupCreate();


    gpio_set_direction(BOOT_GPIO, GPIO_MODE_INPUT);  //boot引脚设为输入检测
    

    ESP_LOGI(TAG, "Create timer handle");
    gptimer_handle_t gptimer = NULL;
    gptimer_config_t timer_config = {
        .clk_src = GPTIMER_CLK_SRC_DEFAULT,
        .direction = GPTIMER_COUNT_UP,
        .resolution_hz = 1000000, // 1MHz, 1 tick=1us
        
    };
    ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer));

    gptimer_event_callbacks_t cbs = {
        .on_alarm = example_timer_on_alarm_cb_v1,
    };

    void *userData = NULL;
    ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, userData));

    ESP_LOGI(TAG, "Enable timer");
    ESP_ERROR_CHECK(gptimer_enable(gptimer));

    ESP_LOGI(TAG, "Start timer, stop it at alarm event");
    gptimer_alarm_config_t alarm_config1 = {
        .reload_count = 0,
        .alarm_count = 1000000, // period = 1s
        .flags.auto_reload_on_alarm = true,
    };
    ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config1));
    ESP_ERROR_CHECK(gptimer_start(gptimer));






}

